package com.lgmshare.k3.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.lgmshare.component.app.LaraApplication;
import com.lgmshare.component.utils.ContextUtils;
import com.lgmshare.component.utils.FileUtils;
import com.lgmshare.component.utils.JSONUtils;
import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.k3.K3Config;
import com.lgmshare.k3.R;
import com.lgmshare.k3.model.Config;

public class UserSettingManager {
    private static final String MY_PREFERENCE = "lep_preference_file";
    private static final String KEY_RUNTIME_MODE = "KEY_RUNTIME_MODE";
    private final static String KEY_CONFIG = "basic_config";
    private static final String KEY_APP_VERSION_CODE = "preference_app_version_code";
    private static final String DATABASE_NAME = "DATABASE_NAME";
    private static final String ENABLE_TEXT_LOGGING = "ENABLE_TEXT_LOGGING";
    private static final String TEXT_LOG_FILE_NAME = "TEXT_LOG_FILE_NAME";
    private static final String SENT_MESSAGE_BACKGROUND_COLOR = "SENT_MESSAGE_BACKGROUND_COLOR";
    private static final String ENABLE_LOGGING_IN_RELEASE_BUILD = "ENABLE_LOGGING_IN_RELEASE_BUILD";

    private Context mContext;
    private SharedPreferences mSharedPreferences;
    private Config mConfig;

    private final static class HolderClass {
        private final static UserSettingManager INSTANCE = new UserSettingManager();
    }

    public static UserSettingManager getInstance() {
        return UserSettingManager.HolderClass.INSTANCE;
    }

    private UserSettingManager() {
        this.mContext = UIUtils.getContext();
        this.mSharedPreferences = UIUtils.getContext().getSharedPreferences(MY_PREFERENCE, Context.MODE_PRIVATE);
    }

    public void setRuntimeMode(int mode) {
        SharedPreferences.Editor edit = mSharedPreferences.edit();
        edit.putInt(KEY_RUNTIME_MODE, mode);
        edit.commit();
    }

    public int getRuntimeMode() {
        int mode = mSharedPreferences.getInt(KEY_RUNTIME_MODE, K3Config.RUNTIME_MODE);
        return mode;
    }

    /**
     * 基础配置
     *
     * @param config
     */
    public void setBasicConfig(Config config) {
        mConfig = config;
        mSharedPreferences.edit().putString(KEY_CONFIG, JSONUtils.toJSONString(mConfig));
    }

    public Config getBasicConfig() {
        if (mConfig == null) {
            String string = mSharedPreferences.getString(KEY_CONFIG, "");
            if (TextUtils.isEmpty(string)) {
                //从本地assetes加载配置文件
                string = FileUtils.readFileFromAssets(mContext, "config");
            }
            mConfig = JSONUtils.parseObject(string, Config.class);
        }
        return mConfig;
    }

    /**
     * 是否是新安装应用（获取后自动更新为false）
     *
     * @return
     */
    public boolean isNewVersion() {
        int currVersionCode = ContextUtils.getVersionCode(mContext);
        int lastVersionCode = mSharedPreferences.getInt(KEY_APP_VERSION_CODE, 0);
        mSharedPreferences.edit().putInt(KEY_APP_VERSION_CODE, currVersionCode);
        return lastVersionCode != currVersionCode;
    }

    public int getColor(String key) {
        return mSharedPreferences.getInt(key, R.color.colorPrimary);
    }

    public int getSentMessageBackgroundColor() {
        return mSharedPreferences.getInt(SENT_MESSAGE_BACKGROUND_COLOR, R.color.colorPrimary);
    }
}
